﻿using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppHynnStateUp;
using Zocono.WCS.Application.AppLogManage;
using Zocono.WCS.Application.AppPlcManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.PlcConnectPoolDomain;
using Zocono.WCS.Infrastructure.ApllicationDto.WmsModel;

namespace Zocono.WCS.HangfireHost.Controllers
{


    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HynnStateController : Controller
    {
        private readonly ILogUtil _LogUtil;
        private readonly LogApp _LogApp;
        private readonly PlcWriteBoMange _plcWriteBoMange;
        private readonly IPlcConnectionPoolService _plcConnectionPoolService;
        private readonly HynnStateUpApp _HynnStateUpApp;

        public HynnStateController(ILogUtil logUtil, LogApp logApp, PlcWriteBoMange plcWriteBoMange, IPlcConnectionPoolService plcConnectionPoolService, HynnStateUpApp hynnStateUpApp)
        {
            _LogUtil = logUtil;
            _LogApp = logApp;
            _plcWriteBoMange = plcWriteBoMange;
            _plcConnectionPoolService = plcConnectionPoolService;
            _HynnStateUpApp = hynnStateUpApp;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> EquipmentState(HynReturnState hynReturnState)
        {
            HeaderResult<bool> result = new();
            try
            {
                InterfaceLogInfo interfaceLogInfo = new InterfaceLogInfo();
                interfaceLogInfo.InterfacePara = $"{JsonConvert.SerializeObject(hynReturnState)}";
                interfaceLogInfo.ActionName = "EquipmentState";
                interfaceLogInfo.StartTime = DateTime.Now;
                interfaceLogInfo.InterfaceUrl = "EquipmentState";
                interfaceLogInfo.Description = "类型：恒翼能上报设备状态";

                var newHynnStateList = new List<HynReturnState>();
                var hynnStateList = _HynnStateUpApp.GetAllHynnStateInfoAsync();
                if (hynnStateList != null && hynnStateList.Count > 0)
                {

                    int isAlarm = hynReturnState.StateCode;
                    string equip = string.Empty;
                    string returnEq = hynReturnState.EquipmentCode;
                    if (returnEq == "P004788")
                    {
                        equip = "250017";
                    }
                    else if (returnEq == "P004787")
                    {
                        equip = "250019";
                    }
                    else if (returnEq == "P004786")
                    {
                        equip = "250021";
                    }
                    else if (returnEq == "P004785")
                    {
                        equip = "250009";
                    }
                    else if (returnEq == "P004784")
                    {
                        equip = "250011";
                    }
                    else if (returnEq == "P004783")
                    {
                        equip = "250013";
                    }
                    else if (returnEq == "P004782")
                    {
                        equip = "250001";
                    }
                    else if (returnEq == "P004781")
                    {
                        equip = "250003";
                    }
                    else if (returnEq == "P004780")
                    {
                        equip = "250005";
                    }
                    else if (returnEq == "P004807")
                    {
                        equip = "250002";
                    }
                    else if (returnEq == "P004808")
                    {
                        equip = "250004";
                    }
                    else if (returnEq == "P004809")
                    {
                        equip = "250006";
                    }
                    else if (returnEq == "P004813")
                    {
                        equip = "100014";
                    }
                    else if (returnEq == "P004812")
                    {
                        equip = "100010";
                    }
                    else if (returnEq == "P004811")
                    {
                        equip = "100006";
                    }
                    else if (returnEq == "P004810")
                    {
                        equip = "100002";
                    }
                    else
                    {
                        equip = returnEq;
                    }

                    hynReturnState.EquipmentCode = equip;
                    var tempState = hynnStateList.Find(h => h.EquipmentCode == hynReturnState.EquipmentCode);
                    //存在设备的状态数据 则替换
                    if (tempState != null)
                    {
                        foreach (var item in hynnStateList)
                        {
                            if (item.EquipmentCode == hynReturnState.EquipmentCode)
                            {
                                newHynnStateList.Add(hynReturnState);
                            }
                            else
                            {
                                newHynnStateList.Add(item);
                            }
                        }
                    }
                    else//不存在直接添加进去
                    {
                        newHynnStateList.Add(hynReturnState);

                        foreach (var item in hynnStateList)
                        {
                            newHynnStateList.Add(item);
                        }
                    }
                }
                else
                {
                    newHynnStateList.Add(hynReturnState);
                }
                _HynnStateUpApp.InsertHynnStateInfoAsync(newHynnStateList);

                ///TODO 不写入PLC 写入会触发报警
                ////报警信息才写日志
                //if (hynReturnState.StateCode > 0)
                //{
                //    await _LogApp.AddInterfaceLogAsync(interfaceLogInfo);
                //}

                ////写入信息到PLC

                //var res = await _plcWriteBoMange.WCSSetPlanerPLCWriteAsync(hynReturnState);
                //if (res)
                //{
                //    result.IsSucceed = true;
                //    result.Result = true;
                //    result.Message = "成功！";

                //    _LogUtil.Info($"恒翼能上报设备状态处理成功！");
                //}
                //else
                //{
                //    result.IsSucceed = false;
                //    result.Result = false;
                //    result.Message = "失败！";

                //    _LogUtil.Info($"恒翼能上报设备状态处理失败！");

                //}
                result.IsSucceed = true;
                result.Result = true;
                result.Message = "上报成功！";
            }
            catch (Exception ex)
            {
                _LogUtil.Error($"恒翼能上报设备状态处理出错，ex={ex.Message}");
                result.IsSucceed = false;
                result.Result = false;
                result.Message = ex.Message;
            }
            return result;
        }

        [HttpGet]
        public async Task<HeaderResult<List<HynReturnState>>> GetHynnStateAsync()
        {
            HeaderResult< List < HynReturnState >> result = new();
            try
            {
                var hynnStateList = _HynnStateUpApp.GetAllHynnStateInfoAsync();
                result.IsSucceed = true;
                result.Result = hynnStateList;
                result.Message = "成功！";
            }
            catch (Exception ex)
            {

                _LogUtil.Error($"获取恒翼能上报设备状态处理出错，ex={ex.Message}");
                result.IsSucceed = false;
                result.Result = null;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}
